<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="pic/pig.jpg" alt="">
        <span class="blog-title">01Blog</span>
        <a class="nav-span" href="blog_list.html">首页</a>
        <a class="nav-span" href="category.html">文章分类</a>
        <a class="nav-span" href="tag.html">文章标签</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <span id="user-info" class="nav-span"></span>
    </div>

    <div class="content-edit">
        <div class="push">
            <input type="text" name="" id="title" placeholder="标题">
            <input type="button" value="发布文章" id="submit" onclick="submit()">
        </div>
        <div class="push">
            <input type="text" name="" id="tag" placeholder="标签">
            <input type="text" name="" id="cage" placeholder="分类">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content" name="content">## 在这里写下一篇博客</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">
        $(function () {
            var editor = editormd("editor", {
                width: "100%",
                height: "550px",
                path: "blog-editormd/lib/"
            });

            // Display user info
            const userInfo = localStorage.getItem('userInfo');
            const userDiv = document.getElementById('user-info');

            if (userInfo) {
                const user = JSON.parse(userInfo);
                userDiv.innerHTML = `
                    <span>用户名: ${user.account}</span>
                    <button onclick="logout()">登出</button>
                `;
            } else {
                userDiv.innerHTML = `
                    <button onclick="redirectTo('login.html')">登录</button>
                    <button onclick="redirectTo('register.html')">注册</button>
                `;
            }
        });

        function redirectTo(page) {
            window.location.href = page;
        }

        function logout() {
            localStorage.removeItem('userInfo');
            window.location.href = 'blog_list.html'; // 返回主页或登录页
        }

        function submit() {
            const userInfo = localStorage.getItem('userInfo');
            let account = '';

            if (userInfo) {
                const user = JSON.parse(userInfo);
                account = user.account;
            }

            $.ajax({
                type: "POST",
                url: "http://localhost:8888/articles/publish",
                contentType: "application/x-www-form-urlencoded",
                data: {
                    title: $("#title").val(),
                    content: $("#content").val(),
                    tag: $("#tag").val(),
                    category: $("#cage").val(),
                    account: account // 确保将 account 也发送到后端
                },
                success: function(result) {
                    if(result.code === 200) {
                        location.href = "blog_list.html";
                    } else {
                        alert("博客发布失败!");
                    }
                },
                error: function() {
                    alert("博客发布失败!");
                }
            });
        }

    </script>
</body>

</html>